
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from data_preprocessing import ML_data_preproccessing
from CF_recommendation import Collaborative_Filtering_recommendation
from MF_recommendation import Matrix_Factorization_SGD
from MF_recommendation import Matrix_Factorization_BGD
from measure import measure_method


if __name__ == '__main__':

    # ML_data_preproccessing.data_preprocess()

    # 导入数据
    # movies_feature = pd.read_csv('../data/Moivelens/ml-latest-small/movies_feature.csv', index_col=0)
    user_rating_train = pd.read_csv('C:\\Users\\lyf\\Desktop\\赵易聪\\大数据分析B\\HW2\\Project2-data\\user-rating_train.csv', index_col=0)
    user_rating_test = pd.read_csv('C:\\Users\\lyf\\Desktop\\赵易聪\\大数据分析B\\HW2\\Project2-data\\user-rating_test.csv', index_col=0)

    # train, test = ML_data_preproccessing.train_test_split(user_rating)
    ratings_index = user_rating_train.index
    ratings_col = user_rating_train.columns
    train = pd.DataFrame(user_rating_train,
                         columns=ratings_col,
                         index= ratings_index)
    test = pd.DataFrame(user_rating_test,
                        columns=ratings_col,
                        index= ratings_index)
    # pad = pd.DataFrame(np.zeros([10000,10000]),
    #                    columns =ratings_col,
    #                    index= ratings_index)

    # # 使用协同过滤算法来估计评分
    # count = 0
    # total = float(train.shape[0])
    # for idx, user in test.iterrows():
    #     unrated_index = user[user > 0].index.values
    #     unrated_index_ = map(int, unrated_index)
    #     rates_lst = Collaborative_Filtering_recommendation.CF_recommend_estimate(train, idx, unrated_index_, 50)
    #
    #     pad.loc[idx, unrated_index] = rates_lst
    #
    #     count += 1
    #     if count % 100 == 0:
    #         presentage = round((count / total) * 100)
    #         print('uu Completed %d' % presentage + '%')
    #
    # pad.to_csv('C:\\Users\\lyf\\Desktop\\赵易聪\\大数据分析B\\HW2\\Project2-data\\pred_ratings_CF.csv')

    # # 计算协同过滤的RMSE
    # pred_CF = pd.read_csv('C:\\Users\\lyf\\Desktop\\赵易聪\\大数据分析B\\HW2\\Project2-data\\pred_ratings_CF.csv', index_col=0)
    # actual_mat = np.array(test)
    # actual_nz_index = actual_mat.nonzero()
    # actual = actual_mat[actual_nz_index[0],actual_nz_index[1]]
    # pred_CF_mat = np.array(pred_CF)
    # pred_CF_nz_index = pred_CF_mat.nonzero()
    # pred_CF = pred_CF_mat[pred_CF_nz_index[0],pred_CF_nz_index[1]]
    # # rand_rate = np.random.randint(1,6,1719466)
    # # rand_rate = np.array([3]*1719466)
    # print('RMSE of CF is %s' % measure_method.comp_rmse(pred_CF, actual))
    # # print('RMSE for rand rate of CF is %s' % measure_method.comp_rmse(rand_rate, actual))


    # 使用矩阵分解算法(SGD/BGD)来估计评分
    # MF_estimate = Matrix_Factorization_SGD.Matrix_Factorization_SGD(K=50, epoch=30)
    MF_estimate = Matrix_Factorization_BGD.Matrix_Factorization_BGD(K=50 ,lamuda=0.01, epoch=30,test=test)
    MF_estimate.fit(train)
    R_hat = MF_estimate.start(test)
    non_index = test.values.nonzero()
    pred_MF = R_hat[non_index[0], non_index[1]]
    actual = test.values[non_index[0], non_index[1]]

    print('the final RMSE of MF is %s' % measure_method.comp_rmse(pred_MF, actual))
